package com.csun.cmny.provider.device.csun.gateway;

import com.csun.cmny.mqtt.entity.*;
import com.csun.cmny.provider.dao.DmcCsunGatewayCmdHiMapper;
import com.csun.cmny.provider.dao.DmcCsunGatewayCmdMapper;
import com.csun.cmny.provider.device.watch.entity.WatchConstant;
import com.csun.cmny.provider.model.constant.DeviceConstant;
import com.csun.cmny.provider.model.domain.*;
import com.csun.cmny.provider.model.dto.gateway.GatewayBindDto;
import com.csun.cmny.provider.model.enums.device.DmcDeviceTypeEnum;
import com.csun.cmny.provider.model.vo.csunversion.VersionSetVo;
import com.csun.cmny.provider.model.vo.gateway.GatewayBindVo;
import com.csun.cmny.provider.service.*;
import com.csun.cmny.provider.utils.DateUtil;
import com.csun.cmny.provider.utils.WatchUtils;
import com.csun.cmny.util.PublicUtil;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-12-30 20:10
 * @Description:
 */
@Slf4j
@Component
public class GatewayMessageSender {

    @Resource
    private CsunGatewayMessageGateway csunGatewayMessageGateway;
    @Resource
    private DmcDeviceService dmcDeviceService;
    @Resource
    private DmcCsunGatewayBindService dmcCsunGatewayBindService;
    @Resource
    private DmcCsunGatewayCmdMapper dmcCsunGatewayCmdMapper;
    @Resource
    private DmcCsunGatewayCmdHiMapper dmcCsunGatewayCmdHiMapper;
    @Resource
    private DmcCsunGatewayConfigHumidistatService dmcCsunGatewayConfigHumidistatService;
    @Resource
    private DmcCsunGatewayConfigBodySensorService dmcCsunGatewayConfigBodySensorService;
    @Resource
    private DmcCsunVersionSetDeviceService dmcCsunVersionSetDeviceService;

    /**
     * send gateway bind humidistat msg
     *
     * @param netgateId         the gateway device no
     * @param humidistatList    the humidistat device no list
     */
    public void bindHumidistat(Long deviceId, String netgateId, List<String> humidistatList) {

//        if (PublicUtil.isEmpty(humidistatList)) {
//            log.error("bind humidistat error, humidistat list is null...");
//        }

        int qos = 0;

        MsgHumidistatBind msgHumidistatBind = new MsgHumidistatBind();

        msgHumidistatBind.setNetgateId(netgateId);
        msgHumidistatBind.setEventId(DateUtil.getDateStr(new Date()));
        msgHumidistatBind.setHumidistatList(humidistatList);
        msgHumidistatBind.setHumidistatNum(humidistatList.size() + "");
        String msg = JSONObject.fromObject(msgHumidistatBind).toString();

        DmcCsunGatewayCmd dmcCsunGatewayCmd = new DmcCsunGatewayCmd();
        dmcCsunGatewayCmd.setDeviceId(deviceId);
        dmcCsunGatewayCmd.setMsgType(msgHumidistatBind.getMsgType());
        dmcCsunGatewayCmd.setDeviceNo(netgateId);
        dmcCsunGatewayCmd.setContent(msg);
        dmcCsunGatewayCmd.setEventId(msgHumidistatBind.getEventId());
        dmcCsunGatewayCmd.setQos(qos);
        dmcCsunGatewayCmd.setSensorNo(netgateId);
        dmcCsunGatewayCmdMapper.saveOrUpdate(dmcCsunGatewayCmd);

        DmcCsunGatewayCmdHi dmcCsunGatewayCmdHi = new DmcCsunGatewayCmdHi();
        dmcCsunGatewayCmdHi.setContent(msg);
        dmcCsunGatewayCmdHi.setDeviceId(deviceId);
        dmcCsunGatewayCmdHi.setDeviceNo(netgateId);
        dmcCsunGatewayCmdHi.setMsgType(msgHumidistatBind.getMsgType());
        dmcCsunGatewayCmdHi.setQos(qos);
        dmcCsunGatewayCmdHi.setEventId(msgHumidistatBind.getEventId());
        dmcCsunGatewayCmdHi.setSensorNo(netgateId);
        dmcCsunGatewayCmdHiMapper.insertSelective(dmcCsunGatewayCmdHi);

        csunGatewayMessageGateway.sendToMqtt(netgateId, qos, msg);
    }

    /**
     * send gateway bind body sensor msg
     *
     * @param netgateId         the gateway device no
     * @param bodySensorList    the body sensor device no list
     * @param type              0-红外 1-欧姆龙
     */
    public void bindBodySensor(Long deviceId, String netgateId, List<String> bodySensorList, String type) {

//        if (PublicUtil.isEmpty(bodySensorList)) {
//            log.error("bind body sensor error, body sensor list is null...");
//        }

        int qos = 0;

        MsgBodySensorBind msgBodySensorBind = new MsgBodySensorBind();

        msgBodySensorBind.setNetgateId(netgateId);
        msgBodySensorBind.setEventId(DateUtil.getDateStr(new Date()));
        msgBodySensorBind.setBodySensorList(bodySensorList);
        msgBodySensorBind.setBodySensorNum(bodySensorList.size() + "");
        msgBodySensorBind.setType(type);

        String msg = JSONObject.fromObject(msgBodySensorBind).toString();

        DmcCsunGatewayCmd dmcCsunGatewayCmd = new DmcCsunGatewayCmd();
        dmcCsunGatewayCmd.setDeviceId(deviceId);
        dmcCsunGatewayCmd.setMsgType(msgBodySensorBind.getMsgType());
        dmcCsunGatewayCmd.setDeviceNo(netgateId);
        dmcCsunGatewayCmd.setContent(msg);
        dmcCsunGatewayCmd.setEventId(msgBodySensorBind.getEventId());
        dmcCsunGatewayCmd.setQos(qos);
        dmcCsunGatewayCmd.setSensorNo(netgateId);
        dmcCsunGatewayCmdMapper.saveOrUpdate(dmcCsunGatewayCmd);

        DmcCsunGatewayCmdHi dmcCsunGatewayCmdHi = new DmcCsunGatewayCmdHi();
        dmcCsunGatewayCmdHi.setContent(msg);
        dmcCsunGatewayCmdHi.setDeviceId(deviceId);
        dmcCsunGatewayCmdHi.setDeviceNo(netgateId);
        dmcCsunGatewayCmdHi.setMsgType(msgBodySensorBind.getMsgType());
        dmcCsunGatewayCmdHi.setQos(qos);
        dmcCsunGatewayCmdHi.setEventId(msgBodySensorBind.getEventId());
        dmcCsunGatewayCmdHi.setSensorNo(netgateId);
        dmcCsunGatewayCmdHiMapper.insertSelective(dmcCsunGatewayCmdHi);

        csunGatewayMessageGateway.sendToMqtt(netgateId, qos, msg);
    }

    /**
     * send humidistat timer msg
     *
     * @param netgateId     the gate device no
     * @param humidistatId  the humidistat device no
     * @param duration      the humidistat update data duration
     */
    public void humidistatTimer(String netgateId, String humidistatId, String duration) {

        MsgHumidistatTimer msgHumidistatTimer = new MsgHumidistatTimer();

        msgHumidistatTimer.setNetgateId(netgateId);
        msgHumidistatTimer.setDuration(duration);
        msgHumidistatTimer.setEventId(DateUtil.getDateStr(new Date()));
        msgHumidistatTimer.setHumidistatId(humidistatId);

        csunGatewayMessageGateway.sendToMqtt(netgateId, 0, JSONObject.fromObject(msgHumidistatTimer).toString());
    }

    public void setBodySensorTimer(MsgLifeBodySensorTimer msgLifeBodySensorTimer) {
        csunGatewayMessageGateway.sendToMqtt(msgLifeBodySensorTimer.getNetgateId(), 0, JSONObject.fromObject(msgLifeBodySensorTimer).toString());
    }

    /**
     * 设置人感报警时间
     * @date 2020/1/7 15:32
     * @param bodySensorId, timeOne, timeTwo, timeThree
     * @return com.csun.cmny.provider.device.csun.gateway.GatewayMessageSender
     */
    public void setLifeBodySensorTimer(String bodySensorId, String timeOne, String timeTwo, String timeThree) {

        /**
         * @author  chisj
         * @date    2020-11-19
         * @desc    人感间隔时间分换算到秒
         */
        timeOne = (Integer.parseInt(timeOne) * 60) + "";
        timeTwo = (Integer.parseInt(timeTwo) * 60) + "";
        timeThree = (Integer.parseInt(timeThree) * 60) + "";

        Long deviceId = Long.valueOf(bodySensorId);

        DmcDevice bodySensor = dmcDeviceService.selectByKey(deviceId);
        if (PublicUtil.isEmpty(bodySensor)) {
            log.error("================人感设备不存在================");
            return ;
        }
        if (!bodySensor.getDeviceType().equals(DmcDeviceTypeEnum.NEWBODY.getIndex()) && !bodySensor.getDeviceType().equals(DmcDeviceTypeEnum.CSUN_BODY_SENSOR.getIndex())) {
            log.error("该设备不是人体感应");
            return ;
        }

        // 通过设备Id查询绑定的网关
        GatewayBindDto gatewayBindDto = new GatewayBindDto();
        gatewayBindDto.setDeviceIdList(Lists.newArrayList(deviceId));
        gatewayBindDto.setBindType(DmcDeviceTypeEnum.CSUN_GATEWAY.getIndex());
        List<GatewayBindVo> gatewayBindVoList = dmcCsunGatewayBindService.queryGatewayList(gatewayBindDto);
        if (PublicUtil.isEmpty(gatewayBindVoList)) {
            log.error("人体感应未绑定网关");
        }

        int qos = 0;
        for (GatewayBindVo gbv : gatewayBindVoList) {

            MsgLifeBodySensorTimer msgLifeBodySensorTimer = new MsgLifeBodySensorTimer();

            msgLifeBodySensorTimer.setBodySensorId(bodySensor.getDeviceNo());
            msgLifeBodySensorTimer.setEventId(DateUtil.getDateStr(new Date()));
            msgLifeBodySensorTimer.setNetgateId(gbv.getDeviceNo());
            msgLifeBodySensorTimer.setTimeOne(timeOne);
            msgLifeBodySensorTimer.setTimeTwo(timeTwo);
            msgLifeBodySensorTimer.setTimeThree(timeThree);

            String msg = JSONObject.fromObject(msgLifeBodySensorTimer).toString();

            DmcCsunGatewayCmd dmcCsunGatewayCmd = new DmcCsunGatewayCmd();
            dmcCsunGatewayCmd.setDeviceId(gbv.getDeviceId());
            dmcCsunGatewayCmd.setMsgType(msgLifeBodySensorTimer.getMsgType());
            dmcCsunGatewayCmd.setDeviceNo(gbv.getDeviceNo());
            dmcCsunGatewayCmd.setContent(msg);
            dmcCsunGatewayCmd.setEventId(msgLifeBodySensorTimer.getEventId());
            dmcCsunGatewayCmd.setQos(qos);
            dmcCsunGatewayCmd.setSensorNo(bodySensor.getDeviceNo());
//            dmcCsunGatewayCmdMapper.saveOrUpdate(dmcCsunGatewayCmd);

            DmcCsunGatewayCmdHi dmcCsunGatewayCmdHi = new DmcCsunGatewayCmdHi();
            dmcCsunGatewayCmdHi.setContent(msg);
            dmcCsunGatewayCmdHi.setDeviceId(gbv.getDeviceId());
            dmcCsunGatewayCmdHi.setDeviceNo(gbv.getDeviceNo());
            dmcCsunGatewayCmdHi.setMsgType(msgLifeBodySensorTimer.getMsgType());
            dmcCsunGatewayCmdHi.setQos(qos);
            dmcCsunGatewayCmdHi.setEventId(msgLifeBodySensorTimer.getEventId());
            dmcCsunGatewayCmd.setSensorNo(bodySensor.getDeviceNo());
//            dmcCsunGatewayCmdHiMapper.insertSelective(dmcCsunGatewayCmdHi);

            // 网关配置重置
            DmcCsunGatewayConfigBodySensor dmcCsunGatewayConfigBodySensor = new DmcCsunGatewayConfigBodySensor();
            dmcCsunGatewayConfigBodySensor.setGatewayId(gbv.getDeviceId());
            dmcCsunGatewayConfigBodySensor.setDeviceId(deviceId);
            dmcCsunGatewayConfigBodySensor = dmcCsunGatewayConfigBodySensorService.selectOne(dmcCsunGatewayConfigBodySensor);
            if (PublicUtil.isEmpty(dmcCsunGatewayConfigBodySensor)) {

                dmcCsunGatewayConfigBodySensor = new DmcCsunGatewayConfigBodySensor();
                dmcCsunGatewayConfigBodySensor.setGatewayId(gbv.getDeviceId());
                dmcCsunGatewayConfigBodySensor.setDeviceId(deviceId);
                dmcCsunGatewayConfigBodySensor.setToiletAttentionTime(Integer.parseInt(timeOne));
                dmcCsunGatewayConfigBodySensor.setToiletWarningTime(Integer.parseInt(timeTwo));
                dmcCsunGatewayConfigBodySensor.setToiletUnusualTime(Integer.parseInt(timeThree));
                dmcCsunGatewayConfigBodySensor.setIsSend(0);
                dmcCsunGatewayConfigBodySensorService.save(dmcCsunGatewayConfigBodySensor);

                dmcCsunGatewayCmdMapper.saveOrUpdate(dmcCsunGatewayCmd);
                dmcCsunGatewayCmdHiMapper.insertSelective(dmcCsunGatewayCmdHi);
                csunGatewayMessageGateway.sendToMqtt(gbv.getDeviceNo(), qos, msg);
            } else {
                if (dmcCsunGatewayConfigBodySensor.getToiletAttentionTime().toString().equals(timeOne)
                        && dmcCsunGatewayConfigBodySensor.getToiletWarningTime().toString().equals(timeTwo)
                        && dmcCsunGatewayConfigBodySensor.getToiletUnusualTime().toString().equals(timeThree)
                        && dmcCsunGatewayConfigBodySensor.getIsSend().equals(1) ) {

                } else {    // 需要下发
                    dmcCsunGatewayCmdMapper.saveOrUpdate(dmcCsunGatewayCmd);
                    dmcCsunGatewayCmdHiMapper.insertSelective(dmcCsunGatewayCmdHi);
                    csunGatewayMessageGateway.sendToMqtt(gbv.getDeviceNo(), qos, msg);
                }
            }
        }
    }

    /**
     * 设置温湿度上传间隔
     *
     * @param deviceId  the humidistat device id
     * @param duration  the humidistat upload duration
     */
    public void setHumidistatTimer(Long deviceId, Integer duration) {

        // 通过设备Id查询设备
        // dmcDeviceService.checkDeviceIsCsunGatewayByDeviceId(deviceId);
        DmcDevice humidistat = dmcDeviceService.selectByKey(deviceId);
        if (PublicUtil.isEmpty(humidistat)) {
            log.error("温湿度器不存在");
            return ;
        }
        if (!humidistat.getDeviceType().equals(DmcDeviceTypeEnum.HUMIDISTAT.getIndex())) {
            log.error("该设备不是温湿度器");
            return ;
        }

        // 通过设备Id查询绑定的网关
        GatewayBindDto gatewayBindDto = new GatewayBindDto();
        gatewayBindDto.setDeviceIdList(Lists.newArrayList(deviceId));
        gatewayBindDto.setBindType(DmcDeviceTypeEnum.CSUN_GATEWAY.getIndex());
        List<GatewayBindVo> gatewayBindVoList = dmcCsunGatewayBindService.queryGatewayList(gatewayBindDto);
        if (PublicUtil.isEmpty(gatewayBindVoList)) {
            log.error("温湿度器未绑定网关");
        }

        int qos = 0;
        for (GatewayBindVo gbv : gatewayBindVoList) {

            MsgHumidistatTimer msgHumidistatTimer = new MsgHumidistatTimer();

            msgHumidistatTimer.setNetgateId(gbv.getDeviceNo());
            msgHumidistatTimer.setDuration(duration.toString());
            msgHumidistatTimer.setEventId(DateUtil.getDateStr(new Date()));
            msgHumidistatTimer.setHumidistatId(humidistat.getDeviceNo());

            String msg = JSONObject.fromObject(msgHumidistatTimer).toString();

            DmcCsunGatewayCmd dmcCsunGatewayCmd = new DmcCsunGatewayCmd();
            dmcCsunGatewayCmd.setDeviceId(gbv.getDeviceId());
            dmcCsunGatewayCmd.setMsgType(msgHumidistatTimer.getMsgType());
            dmcCsunGatewayCmd.setDeviceNo(gbv.getDeviceNo());
            dmcCsunGatewayCmd.setContent(msg);
            dmcCsunGatewayCmd.setEventId(msgHumidistatTimer.getEventId());
            dmcCsunGatewayCmd.setQos(qos);
            dmcCsunGatewayCmd.setSensorNo(humidistat.getDeviceNo());
            // dmcCsunGatewayCmdMapper.saveOrUpdate(dmcCsunGatewayCmd);

            DmcCsunGatewayCmdHi dmcCsunGatewayCmdHi = new DmcCsunGatewayCmdHi();
            dmcCsunGatewayCmdHi.setSensorNo(humidistat.getDeviceNo());
            dmcCsunGatewayCmdHi.setContent(msg);
            dmcCsunGatewayCmdHi.setDeviceId(gbv.getDeviceId());
            dmcCsunGatewayCmdHi.setDeviceNo(gbv.getDeviceNo());
            dmcCsunGatewayCmdHi.setMsgType(msgHumidistatTimer.getMsgType());
            dmcCsunGatewayCmdHi.setQos(qos);
            dmcCsunGatewayCmdHi.setEventId(msgHumidistatTimer.getEventId());
            // dmcCsunGatewayCmdHiMapper.insertSelective(dmcCsunGatewayCmdHi);

            // 网关配置重置
            DmcCsunGatewayConfigHumidistat dmcCsunGatewayConfigHumidistat = new DmcCsunGatewayConfigHumidistat();
            dmcCsunGatewayConfigHumidistat.setGatewayId(gbv.getDeviceId());
            dmcCsunGatewayConfigHumidistat.setDeviceId(deviceId);
            dmcCsunGatewayConfigHumidistat = dmcCsunGatewayConfigHumidistatService.selectOne(dmcCsunGatewayConfigHumidistat);
            if (PublicUtil.isEmpty(dmcCsunGatewayConfigHumidistat)) {

                dmcCsunGatewayConfigHumidistat = new DmcCsunGatewayConfigHumidistat();
                dmcCsunGatewayConfigHumidistat.setGatewayId(gbv.getDeviceId());
                dmcCsunGatewayConfigHumidistat.setDeviceId(deviceId);
                dmcCsunGatewayConfigHumidistat.setDuration(duration);
                dmcCsunGatewayConfigHumidistat.setIsSend(0);
                dmcCsunGatewayConfigHumidistatService.save(dmcCsunGatewayConfigHumidistat);

                dmcCsunGatewayCmdMapper.saveOrUpdate(dmcCsunGatewayCmd);
                dmcCsunGatewayCmdHiMapper.insertSelective(dmcCsunGatewayCmdHi);
                csunGatewayMessageGateway.sendToMqtt(gbv.getDeviceNo(), qos, msg);
            } else {
                if (dmcCsunGatewayConfigHumidistat.getDuration().equals(duration)
                        && dmcCsunGatewayConfigHumidistat.getIsSend().equals(1) ) {

                } else {    // 需要下发
                    dmcCsunGatewayCmdMapper.saveOrUpdate(dmcCsunGatewayCmd);
                    dmcCsunGatewayCmdHiMapper.insertSelective(dmcCsunGatewayCmdHi);
                    csunGatewayMessageGateway.sendToMqtt(gbv.getDeviceNo(), qos, msg);
                }
            }


        }
    }

    /**
     * 发送网关升级指令
     *
     * @param dmcDeviceList         the dmc device list
     * @param dmcCsunVersionFile    the dmc csun version file
     */
    public void sendUpdateVerMsg(List<DmcDevice> dmcDeviceList, DmcCsunVersionFile dmcCsunVersionFile, String fileUrl) {

        int qos = 0;

        for (DmcDevice dmcDevice : dmcDeviceList) {

            MsgUpdateVer msgUpdateVer = new MsgUpdateVer();
            msgUpdateVer.setFileType(dmcCsunVersionFile.getFileType() + "");
            msgUpdateVer.setFileVersion(dmcCsunVersionFile.getFileVersion());
            msgUpdateVer.setLength(dmcCsunVersionFile.getLength() + "");
            msgUpdateVer.setMd5(dmcCsunVersionFile.getMd5());
            msgUpdateVer.setEventId(DateUtil.getDateStr(new Date()));
            msgUpdateVer.setFileUrl(fileUrl);    // 下载地址
            msgUpdateVer.setSn(dmcDevice.getDeviceNo());

            String msg = JSONObject.fromObject(msgUpdateVer).toString();

            DmcCsunGatewayCmd dmcCsunGatewayCmd = new DmcCsunGatewayCmd();
            dmcCsunGatewayCmd.setDeviceId(dmcDevice.getId());
            dmcCsunGatewayCmd.setMsgType(msgUpdateVer.getMsgType());
            dmcCsunGatewayCmd.setDeviceNo(dmcDevice.getDeviceNo());
            dmcCsunGatewayCmd.setContent(msg);
            dmcCsunGatewayCmd.setEventId(msgUpdateVer.getEventId());
            dmcCsunGatewayCmd.setQos(qos);
            dmcCsunGatewayCmd.setSensorNo(dmcDevice.getDeviceNo());
            dmcCsunGatewayCmdMapper.saveOrUpdate(dmcCsunGatewayCmd);

            DmcCsunGatewayCmdHi dmcCsunGatewayCmdHi = new DmcCsunGatewayCmdHi();
            dmcCsunGatewayCmdHi.setSensorNo(dmcDevice.getDeviceNo());
            dmcCsunGatewayCmdHi.setContent(msg);
            dmcCsunGatewayCmdHi.setDeviceId(dmcDevice.getId());
            dmcCsunGatewayCmdHi.setDeviceNo(dmcDevice.getDeviceNo());
            dmcCsunGatewayCmdHi.setMsgType(msgUpdateVer.getMsgType());
            dmcCsunGatewayCmdHi.setQos(qos);
            dmcCsunGatewayCmdHi.setEventId(msgUpdateVer.getEventId());
            dmcCsunGatewayCmdHiMapper.insertSelective(dmcCsunGatewayCmdHi);
        }

    }

    public void sendUpdateVerMsg(List<DmcDevice> dmcDeviceList, VersionSetVo set) {

        int qos = 0;
        List<DmcCsunVersionSetDevice> dmcCsunVersionSetDeviceList = org.python.google.common.collect.Lists.newArrayList();

        for (DmcDevice dmcDevice : dmcDeviceList) {

            MsgUpdateVer msgUpdateVer = new MsgUpdateVer();
            msgUpdateVer.setFileType(set.getFileType() + "");
            msgUpdateVer.setFileVersion(set.getFileVersion());
            msgUpdateVer.setLength(set.getLength() + "");
            msgUpdateVer.setMd5(set.getMd5());
            msgUpdateVer.setEventId(WatchUtils.getRandomString(12).toUpperCase());
            msgUpdateVer.setFileUrl(set.getFileUrl());    // 下载地址
            msgUpdateVer.setSn(dmcDevice.getDeviceNo());

            String msg = JSONObject.fromObject(msgUpdateVer).toString();

            DmcCsunGatewayCmd dmcCsunGatewayCmd = new DmcCsunGatewayCmd();
            dmcCsunGatewayCmd.setDeviceId(dmcDevice.getId());
            dmcCsunGatewayCmd.setMsgType(msgUpdateVer.getMsgType());
            dmcCsunGatewayCmd.setDeviceNo(dmcDevice.getDeviceNo());
            dmcCsunGatewayCmd.setContent(msg);
            dmcCsunGatewayCmd.setEventId(msgUpdateVer.getEventId());
            dmcCsunGatewayCmd.setQos(qos);
            dmcCsunGatewayCmd.setSensorNo(dmcDevice.getDeviceNo());
            dmcCsunGatewayCmdMapper.saveOrUpdate(dmcCsunGatewayCmd);

            DmcCsunGatewayCmdHi dmcCsunGatewayCmdHi = new DmcCsunGatewayCmdHi();
            dmcCsunGatewayCmdHi.setSensorNo(dmcDevice.getDeviceNo());
            dmcCsunGatewayCmdHi.setContent(msg);
            dmcCsunGatewayCmdHi.setDeviceId(dmcDevice.getId());
            dmcCsunGatewayCmdHi.setDeviceNo(dmcDevice.getDeviceNo());
            dmcCsunGatewayCmdHi.setMsgType(msgUpdateVer.getMsgType());
            dmcCsunGatewayCmdHi.setQos(qos);
            dmcCsunGatewayCmdHi.setEventId(msgUpdateVer.getEventId());
            dmcCsunGatewayCmdHiMapper.insertSelective(dmcCsunGatewayCmdHi);

            /**
             * @date    2020-05-13
             * @author  chisj
             * @desc    更新设备升级状态
             */
            DmcCsunVersionSetDevice record = new DmcCsunVersionSetDevice();
            record.setVersionSetId(set.getId());
            record.setDeviceId(dmcDevice.getId());
            record.setStat(DeviceConstant.VERSION_SET_CMD_CREATE);
            record.setTaskId(msgUpdateVer.getEventId());
            dmcCsunVersionSetDeviceList.add(record);
        }

        dmcCsunVersionSetDeviceService.batchUpdate(dmcCsunVersionSetDeviceList);
    }

    /**
     * send gateway bind button msg
     *
     * @param netgateId         the gateway device no
     * @param buttonList        the button device no list
     */
    public void bindButton(Long deviceId, String netgateId, List<String> buttonList) {

        int qos = 0;

        MsgButtonBind msgButtonBind = new MsgButtonBind();

        msgButtonBind.setNetgateId(netgateId);
        msgButtonBind.setEventId(DateUtil.getDateStr(new Date()));
        msgButtonBind.setButtonList(buttonList);
        msgButtonBind.setButtonNum(buttonList.size() + "");
        String msg = JSONObject.fromObject(msgButtonBind).toString();

        DmcCsunGatewayCmd dmcCsunGatewayCmd = new DmcCsunGatewayCmd();
        dmcCsunGatewayCmd.setDeviceId(deviceId);
        dmcCsunGatewayCmd.setMsgType(msgButtonBind.getMsgType());
        dmcCsunGatewayCmd.setDeviceNo(netgateId);
        dmcCsunGatewayCmd.setContent(msg);
        dmcCsunGatewayCmd.setEventId(msgButtonBind.getEventId());
        dmcCsunGatewayCmd.setQos(qos);
        dmcCsunGatewayCmd.setSensorNo(netgateId);
        dmcCsunGatewayCmdMapper.saveOrUpdate(dmcCsunGatewayCmd);

        DmcCsunGatewayCmdHi dmcCsunGatewayCmdHi = new DmcCsunGatewayCmdHi();
        dmcCsunGatewayCmdHi.setContent(msg);
        dmcCsunGatewayCmdHi.setDeviceId(deviceId);
        dmcCsunGatewayCmdHi.setDeviceNo(netgateId);
        dmcCsunGatewayCmdHi.setMsgType(msgButtonBind.getMsgType());
        dmcCsunGatewayCmdHi.setQos(qos);
        dmcCsunGatewayCmdHi.setEventId(msgButtonBind.getEventId());
        dmcCsunGatewayCmdHi.setSensorNo(netgateId);
        dmcCsunGatewayCmdHiMapper.insertSelective(dmcCsunGatewayCmdHi);

        csunGatewayMessageGateway.sendToMqtt(netgateId, qos, msg);
    }
}
